package com.yc.baseapi;

import com.mysql.cj.util.StringUtils;

import java.lang.reflect.Array;
import java.util.*;

public class ArrayTest {
	
	public void testArray() {
		List<String> aa = new ArrayList<String>();
		for (int i = 0; i < 500; i++) {
			aa.add(i+"");
		}
		
		System.out.println("------------");
	}

	public void testBinarySearch() {
		String[] aa = {"11", "22", "33"};
		System.out.println(Arrays.binarySearch(aa, "33"));

	}

	public void testList() {
		List<Integer> t = Arrays.asList(46528, 46527, 46517);
		System.out.println(t.contains(46527));
	}

	public void m2() {
		String key = "222222";
		String pj = "2";
		String entIds = "111,222,333";
		String status = "0";//0删除 1上线
		if (!"222222".equals(key)) {
			System.out.println("key is wrong");
		}
		if ("2".equals(pj)) {
			List<Integer> entList = null;
			if (!StringUtils.isNullOrEmpty(entIds)) {
				entList = new ArrayList<>();

				String[] entArr = entIds.split(",");
				for(String entId : entArr) {
					entList.add(Integer.parseInt(entId));
				}

				if (entList.isEmpty()) {
					entList = null;
				}
			}

			System.out.println("entList=" + entList);
			System.out.println("status=" + "1".equals(status));
		}
	}

	private void m3() {
		List<String> list = Arrays.asList("a", "b", "b", "c");
		Set<String> tmp = new HashSet<>(list);
		System.out.println("tmpSize=" + tmp.size());
	}

	private void m4() {
//		List<String> oldList = Arrays.asList("a,b,c,d".split(","));
		List<String> oldList = new ArrayList<>();
		oldList.add("a");
		oldList.add("b");
		oldList.add("c");
		oldList.add("d");
		List<String> newList = new ArrayList<>();
		newList.add("a");
		newList.add("b");
		newList.add("c");
		newList.add("d");

		List<String> addList = new ArrayList<>();
		getChangeItem(oldList, newList, addList);

	}

	private void getChangeItem(List<String> oldList, List<String> newList, List<String> addList) {
		//加入原集合
		Set<String> tmp = new HashSet<>(oldList);

		if (oldList.size() != tmp.size()) {
			System.out.println("原集合出现重复元素");
		}
		int oldLen = oldList.size();

		//加入新集合
		for (String item : newList) {
			tmp.add(item);
			if (tmp.size() > oldLen) {//新元素
				addList.add(item);
			} else {//不变元素
				oldList.remove(item);
			}
		}

		System.out.println("新增元素：");
		System.out.println(addList);
		System.out.println("删除元素：");
		System.out.println(oldList);
	}

	public void m5() {
		List<Integer> realUgrList = new ArrayList<>();
		Integer[] res = realUgrList.toArray(new Integer[realUgrList.size()]);

	}

	public void m6() {
		String a = "aa,bb,cc";
		String b = "bb,dd";

		String c = a + "," + b;
		Set<String> s = new HashSet<>();
		s.addAll(Arrays.asList(c.split(",")));
		System.out.println(s);
	}


	public void m7() {
		String a = "I'm back!!!";
		System.out.println(a);
	}

	public void m8() {
		HashSet hs = new HashSet();
		HashMap hm = new HashMap();
		LinkedList<String> ll = new LinkedList<>();


	}

	//region 测试list

	public void m9() {
		int count = 50000;

		System.out.println("通过for循环遍历ArrayList：" + getByFor(new ArrayList<>(count), count) + "ms");
		System.out.println("通过for循环遍历LinkedList：" + getByFor(new LinkedList<>(), count) + "ms");

//		System.out.println("通过foreach遍历ArrayList：" + getByForeach(new ArrayList<>(count), count) + "ms");
//		System.out.println("通过foreach遍历LinkedList：" + getByForeach(new LinkedList<>(), count) + "ms");
//
//		System.out.println("通过Iterator遍历ArrayList：" + getByForIterator(new ArrayList<>(count), count) + "ms");
//		System.out.println("通过Iterator遍历LinkedList：" + getByForIterator(new LinkedList<>(), count) + "ms");
	}

	public void m10() {
		ArrayList<String> list = new ArrayList<>();//79    19.86585
//		LinkedList<String> list = new LinkedList<>();//81  18.543301

		long start0 = System.nanoTime();
		for (int i = 0; i < 50000; i++) {
			list.add("onemore-" + i);//16.594361
//			list.add(0, "onemore-" + i);//17.404592
		}
		long start = System.nanoTime();
		System.out.println("--" + ((start - start0) / 1000000.0));
	}

	/**
	 * 通过for循环遍历List
	 *
	 * @param list  list
	 * @param count 遍历元素的个数
	 * @return 所耗费的时间（单位：ms）
	 */
	public static double getByFor(List<String> list, int count) {
		long start0 = System.nanoTime();
		for (int i = 0; i < count; i++) {
			list.add("onemore-" + i);
		}
		String name = "万猫学社";
		long start = System.nanoTime();
		System.out.println("--" + ((start - start0) / 1000000.0));
		for (int i = 0; i < count; i++) {
			if (name.equals(list.get(i))) {
				System.out.println(name);
			}
		}
		long end = System.nanoTime();
		return (end - start) / 1000000.0;
	}

	/**
	 * 通过foreach遍历List
	 *
	 * @param list  list
	 * @param count 遍历元素的个数
	 * @return 所耗费的时间（单位：ms）
	 */
	public static double getByForeach(List<String> list, int count) {
		for (int i = 0; i < count; i++) {
			list.add("onemore-" + i);
		}
		String name = "万猫学社";
		long start = System.nanoTime();
		for (String str : list) {
			if (name.equals(str)) {
				System.out.println(name);
			}
		}
		long end = System.nanoTime();
		return (end - start) / 1000000.0;
	}
	/**
	 * 通过Iterator迭代器遍历List
	 *
	 * @param list  list
	 * @param count 遍历元素的个数
	 * @return 所耗费的时间（单位：ms）
	 */
	public static double getByForIterator(List<String> list, int count) {
		for (int i = 0; i < count; i++) {
			list.add("onemore-" + i);
		}
		String name = "万猫学社";
		Iterator<String> iterator = list.iterator();
		long start = System.nanoTime();
		while (iterator.hasNext()) {
			if (name.equals(iterator.next())) {
				System.out.println(name);
			}
		}
		long end = System.nanoTime();
		return (end - start) / 1000000.0;
	}
	//endregion


	public static void main(String[] args) {
		ArrayTest at = new ArrayTest();
//		at.testArray();

//		at.testBinarySearch();
//		at.testList();
//		at.m2();
//		at.m3();
//		at.m4();
//		at.m5();
//		at.m6();
//		at.m7();
//		at.m9();
		at.m10();
	}

}
